package com.ds.web.controller.applet;

import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ds.common.core.domain.entity.SysDictData;
import com.ds.system.service.ISysDictDataService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ds.common.annotation.Log;
import com.ds.common.core.controller.BaseController;
import com.ds.common.core.domain.AjaxResult;
import com.ds.common.enums.BusinessType;
import com.ds.applet.domain.MiniCase;
import com.ds.applet.service.IMiniCaseService;
import com.ds.common.utils.poi.ExcelUtil;
import com.ds.common.core.page.TableDataInfo;

/**
 * 案例Controller
 *
 * @author liucan
 * @date 2024-03-26
 */
@RestController
@RequestMapping("/applet/case")
public class MiniCaseController extends BaseController {
    @Resource
    private IMiniCaseService miniCaseService;

    @Resource
    private MiniLeagueController miniLeagueController;

    /**
     * 查询案例列表
     */
    @GetMapping("/list")
    public TableDataInfo list(MiniCase miniCase, HttpServletRequest request) {
        startPage();
        miniCase.setToApplet(miniLeagueController.toApplet(request));
        List<MiniCase> list = miniCaseService.selectMiniCaseList(miniCase);
        return getDataTable(list);
    }

    /**
     * 新增案例
     */
    @Log(title = "案例", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MiniCase miniCase, HttpServletRequest request) {
        miniCase.setToApplet(miniLeagueController.toApplet(request));
        return toAjax(miniCaseService.insertMiniCase(miniCase));
    }

    /**
     * 修改案例
     */
    @Log(title = "案例", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MiniCase miniCase, HttpServletRequest request) {
        miniCase.setToApplet(miniLeagueController.toApplet(request));
        return toAjax(miniCaseService.updateMiniCase(miniCase));
    }

    /**
     * 删除案例
     */
    @Log(title = "案例", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(miniCaseService.deleteMiniCaseByIds(ids));
    }

    /**
     * 导出案例列表
     */
    @Log(title = "案例", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MiniCase miniCase) {
        List<MiniCase> list = miniCaseService.selectMiniCaseList(miniCase);
        ExcelUtil<MiniCase> util = new ExcelUtil<MiniCase>(MiniCase.class);
        util.exportExcel(response, list, "案例数据");
    }

    /**
     * 获取案例详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(miniCaseService.selectMiniCaseById(id));
    }

}
